<!-- 分拣中心扫描视图 -->
<style type="text/css">
#stock-order,#stock-info {
	margin-top: 10px;
}
#countStock {
	margin-top: 10px;
}
#countStock p{
	margin-right:10px;
}
#scanForm {
	padding-bottom: 5px;
}
#outStock {
	margin-top: 5px;
}
td a, td a:hover {
	cursor: pointer;
	text-decoration: none;
}
#scanForm .form-group {
	margin-right: 1px;
}
</style>
<div class="pageheader">
	<h2>
		<i class="fa fa-pencil-square"></i>日志管理
	</h2>
	<div class="breadcrumb-wrapper">
		<span class="label">你所在的位置：</span>
		<ol class="breadcrumb">
			<li><a href="/index">ERP管理系统</a></li>
			<li>日志管理</li>
			<li>网站订单日志</li>
			<li class="active">分拣中心扫描</li>
		</ol>
	</div>
</div>
<div class="contentpanel">
<div class="main">
    <form id="scanForm" class="form-inline">
        <div class="form-group">
            <input placeholder="扫描或输入条形码" type="text" name="barcode" value="<?php echo $this->barcode;?>" class="form-control" name="title">
        </div>
        <button type="submit" class="btn btn-primary">确定</button>
        <div class="form-group">
            <span class="text-danger">
                <?php 
                if($this->msg=='success'){
                	echo '产品出库成功';
                }else if($this->msg=='fail'){
                	echo '产品出库失败';
                }else{
                    echo $this->msg;
                }?>
            </span>
        </div>     
    </form>
    <form id="addScan" method="post" action="/log/scansort">
        <div class="form-inline">
            <?php if($this->scanByOrder == 'yes'){?>
            <input type="text" name="orderid" readonly="readonly" class="form-control" value="<?php echo $this->oid;?>" placeholder="订单号">
            <input type="text" name="site" class="form-control" readonly="readonly" value="<?php echo $this->site;?>" placeholder="下单网站">
            <div class="form-group" role="alert">
                                扫描完成后，系统会自动匹配订单信息：若完全匹配，则订单显示<span class="label label-success">绿色</span>；若部分匹配，则订单显示<span class="label label-warning">黄色</span>；若不匹配，则扫描项显示<span class="label label-danger">红色</span>
            </div>
            <select class='form-control hidden' name='site'>
                <option value='<?php echo $this->site;?>' selected = "selected"><?php echo $this->site;?></option>
            </select>
            <?php }else if($this->scanByOrder == 'no'){?>
            <input type="text" name="orderid" class="form-control" placeholder="订单号">
            <select class='form-control' name='site'>
                <option value='0'>请选择网站</option>
                <option value='gogoalshop.com'>gogoalshop.com</option>
		        <option value='jerseybbs.com'>jerseybbs.com</option>
		        <option value='goaljerseys.com'>goaljerseys.com</option>
	     	    <option value='fancybestdeal.com'>fancybestdeal.com</option>
	    	    <option value='elmontyouthsoccer.com'>elmontyouthsoccer.com</option>
	    	    <option value='bestcheapsoccer.com'>bestcheapsoccer.com</option>
	    	</select>
            <?php }?>
		</div>
        <div class="table-responsive">
            <?php if($this->scanByOrder == 'yes'){?>
            <table id="stock-order" class="table table-bordered">
                <thead>
                    <tr>
                        <th>订单号</th>
                        <th>SKU</th>
                        <th>产品名称</th>
                        <th>尺码和数量</th>
                        <th>网站</th>
                        <th>时间</th>
                        <th>状态</th>
                    </tr>
                </thead>
                <tbody>
                <?php if ($this->logList):?>
                <?php foreach ($this->logList as $v):?>
                <tr id="<?php echo $v['sku'];?>">
				    <td><?php echo $v['order_id']; ?></td>
                    <td><a onclick="printBarcode('<?php echo $v['sku'];?>');"><?php echo $v['sku'];?></a><input type="text" hidden="hidden" value="<?php echo $v['sku'];?>" name="o_sku[]"></td>
                    <td><?php echo $v['title'];?></td>
                    <td>
                        <?php
                        foreach(json_decode($v['info'], true) as $_v){
                        	$act = '';
                        	if ($v['info_stock'] == 'add') {
                        		$act = '+';	
                        	} else {
                        		$act = '-';	
                        	}
                        	echo '尺码: ' . $_v[0] . ', 库存: ' . $act . stripslashes($_v[1]) . '<br/>';
                        ?>
                        <input type="text" hidden="hidden" value="<?php echo $_v[0];?>" name="o_size[<?php echo $v['sku'];?>][]">
                        <!-- <input class="size_num_<?php echo $_v[0];?>" name="onum_<?php echo $v['sku'];?>_<?php echo $_v[0];?>" type="text" hidden="hidden" value="<?php echo $_v[1];?>">
                        <input class="check_num_<?php echo $_v[0];?>" name="cnum_<?php echo $v['sku'];?>_<?php echo $_v[0];?>" hidden="hidden" type="text" value="0"> -->
                        <?php }?>
                        <?php 
                        foreach (json_decode($v['lack'],true) as $vl){
                        	if(($vl[1]>0) && $v['statu']){
                        		echo '<p style="display:inline;" class="text-danger">缺:'.$vl[0].'/'.$vl[1].'&nbsp;&nbsp;</p>';
                        		echo '<input class="size_num_'.$vl[0].'" name="onum_'.$v['sku'].'_'.$vl[0].'" type="text" hidden="hidden" value="'.$vl[1].'">';
                        		echo '<input class="check_num_'.$vl[0].'" name="cnum_'.$v['sku'].'_'.$vl[0].'" type="text" hidden="hidden" value="0">';
                        	}else{
                        		echo '<input class="size_num_'.$vl[0].'" name="onum_'.$v['sku'].'_'.$vl[0].'" type="text" hidden="hidden" value="'.$vl[1].'">';
                        		echo '<input class="check_num_'.$vl[0].'" name="cnum_'.$v['sku'].'_'.$vl[0].'" type="text" hidden="hidden" value="0">';
                        	}
                        }
                        ?>
				    </td>
				    <td><?php echo $v['site'];?></td>
				    <td><?php echo $v['add_time']; ?></td>
				    <td>
				        <?php
				        switch ($v['statu'])
				        {
				        	case 1:
				        		echo '<span class="status label label-danger">分拣中、缺货中</span>';
				        		break;
				        	case 2:
				        		echo '<span class="status label label-info">分拣完、未处理</span>';
				        		break;
				        	case 3:
				        		echo '<span class="status label label-primary">分拣完、处理中</span>';
				        		break;
				        	case 4:
				        		echo '<span class="status label label-success">分拣完、处理完</span>';
				        		break;
				        	default:
				        		echo '<span class="status label label-warning">未分拣、未处理</span>';
				        }
				        ?>
				    </td>
			    </tr>
			    <?php endforeach;?> 
			    <?php endif;?>
                </tbody>
            </table>
            <?php }?>
            <table id="stock-info" class="table table-bordered">
                <thead>
                    <tr>
                        <th><input type="checkbox" name="checkall">&nbsp;&nbsp;全选</th>
                        <th>SKU</th>
                        <th>产品名称</th>
                        <th>尺码和数量</th>
                        <th>成本价</th>
                        <th>市场价</th>
                        <th>赠品</th>
                        <th>操作时间</th>
                        <th>操作员</th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
        <input class="btn btn-primary" type="submit" value="保存分拣">
        <input class="btn btn-default" type="button" value="删除" onclick="delscanlog();">
    </form>
    <form id="outStock" method="post" action="/log/alloutstock">
        <input type="hidden" name="orderid" value="<?php echo $this->oid;?>"/>
        <input type="hidden" name="site" value="<?php echo $this->site;?>"/>
        <input class="btn btn-warning" type="submit" value="全缺货登记">
    </form>
    <div id="countStock" class="form-inline"></div>
</div>
</div>

<script type="text/javascript">
$("input[name='barcode']").focus();//输入框获取焦点
$("#scanForm").submit( function () {//扫描后异步生成条形码包含的信息内容插入到table中显示
	var barcode = $("input[name='barcode']").val();
	$("input[name='barcode']").val('');
	$("#scanForm .text-danger").text('');
	if(barcode == ''){
		alert('请扫描条形码或直接输入条形码条码');
	}else{
		$.get("/log/exloadstock", {code:barcode},
		    function(data){
		        if(data.size == '' || data.sku == ''){
		        }else{
		            $("#stock-info tbody").append(data.content);
				    <?php if($this->scanByOrder == 'yes'){?>
		            checkScanRight($.trim(data.sku),$.trim(data.size));//检查扫描的条形码信息是否和给出的订单信息匹配
		            <?php }?>
		            $("input[name='barcode']").focus();//输入框获取焦点
			    	countStock();
		        }
		    },'json'
	    );
	}
	return false;
} );
$("#addScan").submit( function () {
	var i = 0;
	var orderid = $("input[name='orderid']").val();
	var site = $("select[name='site']").val();
	$("input[name='issort[]']").each(function (j) {
		if($(this).is(":checked")){
			i+=1;//如果被选中就将i加1
			/** 给input[name='issort[]']进行排序，以方便提交到action好处理,即选中table表中的第几行，
			  * 就赋值到对应的input checkbox的值为第几，然后再将该值提交上去，从而可以判断其他一起提交上去的数组哪一个是被选中了的。
			  */
			$(this).val(j);
		}
    });
    if(orderid == ''){
        alert("请填写订单号！");
        return false;
    }
    if(site == 0){
        alert("请选择网站！");
        return false;
    }
    if(i>0){
        var order_num = $("#stock-order tbody tr").length;
        var order_num_s = $("#stock-order tbody tr.alert-success").length;
        var select_num_s = $("#stock-info tbody tr.alert-success").length;
        if(order_num_s<order_num){
            alert("根据你的选择，网站"+site+"的订单号"+orderid+" 可能存在产品缺货，是否确定保存分拣并将该订单显示为缺货状态");
            return true;
        }else{
            if(i<select_num_s){
                alert("根据你的选择，货齐，请检查扫描成功项，选择完该订单包含的所有产品，否则该订单按缺货处理");
                return false;
            }else{
                return true;
            }
        }
    }else{
        alert("请先扫描，若已扫描则选择确定进行分拣的产品！");
        return false;
    }
});
function delscanlog(){
	$("input[name='issort[]']").each(function () {
		//如果被选中
		if($(this).is(":checked")){
			$(this).parent('td').parent('tr').remove();//就删除
			countStock();
		}
    });
}

function checkScanRight(sku,size){
	var checkSku = false;//定义检查Sku结果初始值为false
	var checkSize = false;//定义检查Size结果初始值为false
	var checkNum = false;//定义检查尺码数量，默认为false
	var sizeAll = true;//定义是否检查了所有尺码，默认为true
	var size_num = parseInt($("input[name='onum_"+sku+"_"+size+"']").val());//该尺码总数量
	var check_num = parseInt($("input[name='cnum_"+sku+"_"+size+"']").val());//该尺码已检查数

	/**扫描后页面滚动到底部*/
    var offset = $("#outStock").offset();
    $("body,html").animate({"scrollTop": offset.top+"px"}, 500); 
	
	/**检查SKU是否匹配*/
	$("input[name='o_sku[]']").each(function(i){
		if($(this).val()==sku){
			checkSku = true;
		}
	});

	/**检查尺码是否匹配*/
	$("input[name='o_size["+sku+"][]']").each(function(i){
		if($(this).val()==size){//检查指定SKU下的指定尺码是否匹配
			checkSize = true;
		}
	});
	
    if(isNaN(size_num)&&isNaN(check_num)){
        /**扫描得出的SKU或者尺码不对*/
    }else{//检查指定SKU下的指定尺码是否检查完
		$("input[name='cnum_"+sku+"_"+size+"']").val(check_num+1);
    	if(check_num+1 < size_num){
        	
    	}else{
    		checkNum = true;
    	}

    	/**遍历该SKU下的所有尺码，看看是否所有尺码都检查完了*/
    	$("input[name='o_size["+sku+"][]']").each(function(i){
    		var size_num = parseInt($(this).siblings(".size_num_"+$(this).val()).val());//该尺码总数量
    		var check_num = parseInt($(this).siblings(".check_num_"+$(this).val()).val());//该尺码已检查数
    		if(check_num < size_num){
    			sizeAll = false;
    		}else{
    			sizeAll = true;
    		}
    	});
    }
    
	if(checkSize && checkSku){//扫描传入的SKU和尺码是否都正确
		$("#stock-info tr:last").addClass("alert alert-success");
		if(checkNum && sizeAll){//尺码是否都检查完了
			$("#"+sku).removeClass("alert alert-warning");
			$("#"+sku).addClass("alert alert-success");
			$("#"+sku+" .status").addClass("label-success");
		}else{
			$("#"+sku+" .status").addClass("label-warning");
			$("#"+sku).addClass("alert alert-warning");
		}
	}else{
		$("#stock-info tr:last").addClass("alert alert-danger");
	}
}
//统计库存
function countStock(){
    var skus =[];
    $("#stock-info tbody tr").each(function(i){
        skus[i] = $(this).children("td").children("input[name='title[]']").val();
    });
    var sizes =[];
    $("#stock-info tbody tr").each(function(i){
        sizes[i] = $(this).children("td").children("input[name='size[]']").val();
    });

    var skusize = [];
    for(var i = 0;i<skus.length;i++){
        skusize[i] = "产品:"+skus[i]+' 尺码:'+sizes[i];
    }
    
    var res = [];
    for(var i = 0;i<skusize.length;i++){
        var c = 0;
    	for(var j = 0;j<skusize.length;j++){
        	if(skusize[i] == skusize[j]){
            	c++;
        	}
    	}
    	res.push(skusize[i]+"/"+c);
    }
    var arr = res.unique();
    //console.log(arr);
    var content = '';
    for(var k =0;k<arr.length;k++){
        content = content+"<p class='label label-success form-group'>"+arr[k]+"</p>";
    }
    $("#countStock").html(content);
}
Array.prototype.unique = function() {
	var ret = [];
	var o = {};
	for(var i=0, len=this.length; i<len; ++i){
		if(!o[this[i]]){ 
			ret.push(this[i]); 
			o[this[i]] = this[i]; 
		}
	}
	return ret; 
}
function printBarcode(sku){
    window.open("/stock/print?sku="+sku,"打印"+sku,"scrollbars=yes, resizable=yes,width=1200,screenX=200");
}
</script>